'''
Created on Jan 9, 2012

@author: alebalbin
Series_sample_id
!Sample_source_name_ch1
!Sample_characteristics_ch1
!Sample_description
!series_matrix_table_begin # Data starts
'''

def read_microarray_matrix(file):
    
    ifile=open(file)
    of=open(file+'.dat','w')
    desp=["!Series_sample_id","!Sample_source_name_ch1",
          "!Sample_characteristics_ch1","!Sample_description",
          "!series_matrix_table_begin"]
    First=False
    for l in ifile:
        f=l.strip('\n').split('\t')
        if f[0]=="!series_matrix_table_begin":
            First=True
            continue
        if First:
            
            of.write(",".join(f).replace(',','\t')+'\n')
            
            






def find_repeated_features(features):
    '''
    '''
    dind=np.array(range(len(features)))
    d=np.array([features.count(i) for i in features])
    return list(d[d>1])
    
def summaryze_repeat_features(mat, gids):
    '''
    summaryze repeated features using the features with highest sum across samples
    '''
    nmat = np.empty( (len(gids), mat.shape[1]) )
    ngids=defaultdict(list)
    i=0
    for f,rows in gids.iteritems():
        rows=np.array(rows)
        s = np.sum(mat[rows,],axis=1)
        s = rows[s==np.max(s)]
        if len(s)>1:
            s=s[0]
        #nmat[i,:]=np.mean(mat[rows,],axis=0) tp get the mean summary
        nmat[i,:]=mat[s,:]
        ngids[f].append(i)
        i+=1
    return nmat, ngids

    
def read_mat(nfile,fcol, cind,offset):
    '''
    '''
    gids,h=read_ids(nfile)
    mat = np.loadtxt(nfile,skiprows=1,usecols=set(range(fcol,len(h) )))
    mat,gids = summaryze_repeat_features(mat, gids)
    #
    fd=calc_fold_change(mat,offset, cind)
    mat = np.append(mat,np.reshape(fd,(mat.shape[0],-1)),axis=1)
    
    return mat, gids


file="/Users/alebalbin/Desktop/nsclc_kras_dep/Data/transcriptomics/GSE3141_series_matrix0.txt"
read_microarray_matrix(file)
